package com.sc.outpatientsys.controller;

import com.sc.outpatientsys.entity.Doctor;
import com.sc.outpatientsys.service.IDoctorService;
import com.sc.outpatientsys.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/doctor")
@Slf4j
public class DoctorController extends BaseController{

    @Autowired
    private IDoctorService doctorService;
    @RequestMapping("/addDoctor")
    public JsonResult<Void> addDoctor(Doctor doctor){
        doctorService.reg(doctor);
        log.info(doctor.toString());
        return new JsonResult<>(OK);
    }


    @RequestMapping("/login")
    public JsonResult<Doctor> login(String username, String password, HttpSession session){
        Doctor data = doctorService.login(username,password);
        session.setAttribute("id",data.getDc_ID());
        session.setAttribute("username",data.getUserName());
       log.info(getIdFromSession(session).toString());
       log.info(getUserNameFromSession(session));
        return new JsonResult<Doctor>(OK,data);
    }
    @RequestMapping("/findById")
    public JsonResult<Doctor> findById(int dc_ID){
        Doctor data = doctorService.findById(dc_ID);
        return new JsonResult<Doctor>(OK,data);
    }
    @RequestMapping("/findByName")
    public JsonResult<Doctor> findByName(String username){
        Doctor data = doctorService.findByName(username);
        return new JsonResult<Doctor>(OK,data);
    }
    @RequestMapping("/findAll")
    public JsonResult<List<Doctor>> findALl(){
        List<Doctor> data = doctorService.findAll();
        return  new JsonResult<>(OK,data);
    }
    @RequestMapping("/update")
    public JsonResult<Void> update(Doctor doctor){
        doctorService.update(doctor);
        return new JsonResult<>(OK);
    }
    @RequestMapping("/delete")
    public JsonResult<Void> delete(int dc_ID){
        doctorService.delete(dc_ID);
        return new JsonResult<>(OK);
    }

    @RequestMapping("/updateAccess")
    public JsonResult<Void> updateAccess(int dc_ID,int access){
        doctorService.updateAccess(dc_ID,access);
        return new JsonResult<>(OK);
    }

    @RequestMapping("/findByClinic")
    public JsonResult<List<Doctor>> findByClinic(String clinic){
        List<Doctor> data = doctorService.findByClinic(clinic);
        return  new JsonResult<>(OK,data);
    }
}
